package models

import (
	"gorm.io/gorm"
	"order_srv/basic/config"
	"time"
)

// OperationLog 操作日志表
type OperationLog struct {
	ID          uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;autoIncrement;not null;" json:"id"`
	UserID      uint64         `gorm:"column:user_id;type:bigint UNSIGNED;comment:操作用户ID;not null;" json:"user_id"`
	Username    string         `gorm:"column:username;type:varchar(255);comment:操作用户名;default:NULL;" json:"username"`
	UserRole    uint8          `gorm:"column:user_role;type:tinyint UNSIGNED;comment:用户角色: 1乘客 2司机 3管理员;default:NULL;" json:"user_role"`
	Operation   string         `gorm:"column:operation;type:varchar(255);comment:操作类型（如：订单导出）;not null;" json:"operation"`
	Resource    string         `gorm:"column:resource;type:varchar(255);comment:操作资源（如：订单数据）;default:NULL;" json:"resource"`
	Params      string         `gorm:"column:params;type:text;comment:操作参数（JSON格式）;default:NULL;" json:"params"`
	IPAddress   string         `gorm:"column:ip_address;type:varchar(50);comment:IP地址;default:NULL;" json:"ip_address"`
	UserAgent   string         `gorm:"column:user_agent;type:text;comment:用户代理;default:NULL;" json:"user_agent"`
	Result      bool           `gorm:"column:result;type:tinyint(1);comment:操作结果：true成功 false失败;default:1;" json:"result"`
	Description string         `gorm:"column:description;type:text;comment:操作描述;default:NULL;" json:"description"`
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

// Create 创建操作日志
func (log *OperationLog) Create() error {
	return config.DB.Create(&log).Error
}
